/**
 * @copyright Copyright (c) 2025 CSG Digital Grid Technology (Guangdong) Co., Ltd.
 * @author Chen Yongjie
 */
#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>

#include <test_string2json.h>
//test target header
#include <libgen.h>  // For dirname
#include <stdlib.h>       // For free
#include "../src/common/cJSON.h"

static cJSON *string2Json(u_int8_t *str)
{

    if (str == NULL)
    {
        return NULL;
    }

    cJSON *ret = cJSON_Parse((char *)str);
    if (ret == NULL)
    {
        printf("string2Json: ret == NULL!\n");
        return NULL;
    }
    else
    {
        if (ret->type != cJSON_Object)
        {
            printf("string2Json: ret->type error!\n");
            return NULL;
        }
    }

    return ret;
}


// 测试用例：字符串为空
void test_string2Json_null(void) {
    CU_ASSERT_PTR_NULL(string2Json(NULL));
}

// 测试用例：无效的 JSON 字符串
void test_string2Json_invalid(void) {
    u_int8_t invalid_json[] = "invalid JSON";
    CU_ASSERT_PTR_NULL(string2Json(invalid_json));
}

// 测试用例：有效的 JSON 字符串，但不是对象
void test_string2Json_not_object(void) {
    u_int8_t valid_json_array[] = "[1, 2, 3]";
    CU_ASSERT_PTR_NULL(string2Json(valid_json_array));
}

// 获取源文件所在目录的路径
static void get_source_dir(char *path, size_t size) {
    // __FILE__ 给出当前文件的完整路径
    char *file_path = strdup(__FILE__);
    char *dir = dirname(file_path);
    strncpy(path, dir, size - 1);
    path[size - 1] = '\0';
    free(file_path);
}

// 从文件读取 JSON 字符串并解析
static cJSON *string2JsonFromFile(const char *filename)
{
    char fullfilename[1024];
    char source_dir[1024];
    get_source_dir(source_dir, sizeof(source_dir));
    snprintf(fullfilename, sizeof(fullfilename), "%s/%s", source_dir, filename);
    FILE *file = fopen(fullfilename, "rb");
    if (file == NULL)
    {
        printf("Unable to open file: %s at %s\n", fullfilename, source_dir);
        return NULL;
    }

    fseek(file, 0, SEEK_END);
    long length = ftell(file);
    fseek(file, 0, SEEK_SET);

    char *data = (char *)malloc(length + 1);
    if (data == NULL)
    {
        fclose(file);
        printf("Unable to allocate memory for file content\n");
        return NULL;
    }

    fread(data, 1, length, file);
    data[length] = '\0';
    fclose(file);

    cJSON *json = cJSON_Parse(data);
    free(data);

    if (json == NULL)
    {
        printf("Error parsing JSON from file: %s\n", filename);
        return NULL;
    }

    return json;
}
// 测试用例：从文件读取有效的 JSON 对象并验证键和值
void test_string2JsonFromFile_valid(void) {
    const char *filename = "AppProfile.json";

    cJSON *json = string2JsonFromFile(filename);
    CU_ASSERT_PTR_NOT_NULL(json);

    if (json) {
        // 验证 JSON 对象类型
        CU_ASSERT_EQUAL(json->type, cJSON_Object);

        // 验证键 "key" 的值
        cJSON *key_item = cJSON_GetObjectItem(json, "appInfo");
        CU_ASSERT_PTR_NOT_NULL(key_item);

        // 验证键 "appname" 的值
        cJSON *app_name = cJSON_GetObjectItem(key_item, "appname");
        CU_ASSERT_PTR_NOT_NULL(app_name);
        if (app_name) {
            CU_ASSERT_EQUAL(app_name->type, cJSON_String);
            CU_ASSERT_STRING_EQUAL(app_name->valuestring, "sys.apptest");
        }

        // 删除 JSON 对象
        cJSON_Delete(json);
    }
}